package eventservice.impl;

import java.rmi.RemoteException;
import java.rmi.Remote;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.List;

import eventservice.Event;
import eventservice.Pullable;
import eventservice.Pushable;
import eventservice.RemotelyPullable;

public class PullConsumerProxyImpl implements Remote, RemotelyPullable, Pushable {

	private static final long serialVersionUID = 1L;
	//private final BlockingQueue<Event> events;
	private List<Event> events;
	private int buffersize;

	PullConsumerProxyImpl(int buffersize)
			throws RemoteException {
		super();
		events = new ArrayList<Event>();
		//events = new ArrayBlockingQueue<Event>(bufferSize);
		this.buffersize = buffersize;
	}

	// Ezt a fogyaszto hivja
	public Event pull() throws RemoteException {
		Event e = null;
		synchronized (events) {
			while (events.isEmpty()) {
				try {
					events.wait();
				} catch (InterruptedException ie) {
				}
			}
			e = (Event) events.remove(0);
		}
		return e;
	}

	// Ezt az EventChannelImpl hivja
	public void push(Event event) throws RemoteException {
		synchronized (events) {
			if (events.size() >= buffersize) {
				events.remove(0);
			}
			events.add(event);
			System.err.println("Debug: PullConsumerProxyImpl added a new Event");
			events.notify();
		}
	}
}
